<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<link rel="stylesheet" href="static/css/tcommon.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>设备管理</title>
</head>

<body>
	<div class="content-container" data-options="region:'center'">
		<div class="panel-header">
			<div class="panel-title">设备管理列表</div>
		</div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>

		<div id="pro_tb">
			<div>
				项目：
				<input style="width: 200px;" id="projectitemid" name="projectitemid" class="easyui-combobox"
					editable="false"
					data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: 'projectitemlist.do?status=2',
							    onLoadSuccess : function(data){ 
							   		if(data.length>0){
							    		$('#projectitemid').combobox('setValue',data[0].id);
							    	}
							    },onSelect: function(rec){
							    	projectitemid = rec.id;
							    	loadData()
							    },panelHeight: 'auto'">
			</div>
			<div>
				<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
				<a href="javascript:void(0)" class="d-btn btn-green" onclick="updatepro()">编辑</a>
				<a href="javascript:void(0)" class="d-btn btn-red" onclick="deletepro()">删除</a>

			</div>
		</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog"
		style="width: 800px; padding: 10px 20px; max-height: 600px; overflow-y: auto; top: 30px;" closed="true"
		buttons="#pro-dlg-buttons">
		<div class="title"></div>
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 200px;">项目:</td>
						<td>
							<input style="width: 200px;" id="projectitem" name="projectitem" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							   	required:true,
							    url: 'projectitemlist.do?status=2',
							    onSelect: function(rec){
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">名称：</td>
						<td colspan="3">
							<input class="easyui-textbox" data-options="required:true" id="name" name="name" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">开机维保时长（小时）：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="startinterval" name="startinterval"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">日常维保时长（天）：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="dailyinterval" name="dailyinterval"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">传感器序列号：</td>
						<td>
							<input class="easyui-textbox" data-options="" id="code" name="code"
								style="width: 200px;" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton ok" onclick="savePro()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">关闭</a>
	</div>
	<div id="proDlg2" class="easyui-dialog info_dialog"
		style="width: 800px; padding: 10px 20px; max-height: 600px; overflow-y: auto; top: 30px;" closed="true"
		buttons="#pro-dlg-buttons2">
		<div class="title"></div>
		<form id="proFm2" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 200px;">间隔时间（分钟）：</td>
						<td>
							<input class="easyui-textbox" data-options="validType:'length[1,4]',required:true" id="timeblank" name="timeblank"
								style="width: 200px;" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons2">
		<a href="# " class="easyui-linkbutton ok" onclick="saveBlank()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg2').dialog('close')">关闭</a>
	</div>
	<div style="display: none">
		<div id="displayAtt" class="datagrid-toolbar"
			style="width: 80%; max-height: 80%; padding: 5px; top: 30px; text-align: center;"></div>
	</div>

</body>
<script>
	var currentPro;
	var projectitemid;
	function loadData() {
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#pro_tb',
			url : "equipmentList",
			method : 'get',
			queryParams : {
				projectitemid : projectitemid
			},
			onSelect : function(index,row) {
				currentPro = row;
			},
			onDblClickRow : function(index,row) {
				currentPro = row;
				updatepro();
			},
			columns : [ [ 
			/**{
				field : 'title',
				title : '项目名',
				width : '200px',
				formatter : function(value,row,index) {
					if (row.item){
						return row.item.name;
					}
				}
			},*/{
				field : 'name',
				title : '名称',
				width : '200px'
			},{
				field : 'startinterval',
				title : '开机维保时长（小时）',
				width : '180px'
			},{
				field : 'dailyinterval',
				title : '日常维保时长（天）',
				width : '180px'
			},{
				field : 'code',
				title : '传感器序列号',
				width : '200px'
			},{
				field : 'timeblank',
				title : '上传间隔',
				width : '150px',
				formatter : function(value,row,index) {
					if (value){
						return value+"分钟" +'<a href="javascript:void(0)" class="d-btn btn-yellow" style="float: right;" onclick="setblank('+index+')">修改</a>';
					}else{
						return '<a href="javascript:void(0)" class="d-btn btn-yellow" style="float: right;"  onclick="setblank('+index+')">修改</a>';
					}
				}
			},{
				field : 'id',
				title : '查看压力记录',
				width : '150px',
				formatter : function(value,row,index) {
					return '<a href="javascript:void(0)" class="d-btn btn-yellow"  onclick="addTab('+index+')">查看</a>';
				}
			} ] ]
		});
	}
	function refreshProTable() {
		$('#pro_table').datagrid("load", {
			projectitemid : projectitemid
		});
	}
	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '添加设备');
		$("#proFm").form('clear');
		currentPro = {};
		$("#projectitem").combobox("enable");
		$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
	}
	function updatepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			$("#projectitem").combobox("setValue", currentPro.item.id);
			$("#projectitem").combobox("disable");
			$("#proFm").form('load', {
				name : currentPro.name,
				startinterval : currentPro.startinterval,
				dailyinterval : currentPro.dailyinterval,
				code:currentPro.code
			});
			$("#proDlg").dialog('open').dialog('setTitle', '更新设备');
			$("#pro-dlg-buttons").find(".easyui-linkbutton.ok").show();
		}else{
			$.messager.alert('更新', '请选择要更新的设备', 'warning');
		}
	}
	function setblank(index){
		currentPro = $("#pro_table").datagrid('getData').rows[index];
		if(currentPro){
			$("#proDlg2").dialog('open').dialog('setTitle', '更新设备');
			$("#proFm2").form('clear');
			$("#proFm2").form('load', {
				blank : currentPro.timeblank,
			});
		}
	}
	
	function saveBlank(){
		if (!$("#proFm2").form('validate'))
			return;
		$.ajax({
			type : 'POST',
			url : "equipmentSetBlank.do?id="+currentPro.id+"&blank="+$("#timeblank").textbox("getValue"),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存设备',
						msg : '保存设备成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
				}else{
					$.messager.alert('保存设备', '保存设备失败', 'error');
				}
				$('#proDlg2').dialog('close');
			},
			dataType : "json"
		});
	}
	function deletepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
				if (data){
					$.ajax({
						type : 'POST',
						url : "equipmentDelete.do?id=" + currentPro.id,
						contentType : "application/json",
						success : function(data) {
							if (data.success){
								$.messager.show({
									title : '删除',
									msg : '删除成功',
									timeout : 5000,
									showType : 'slide'
								});
								refreshProTable();
							}else{
								$.messager.alert('删除', '删除失败', 'error');
							}
						},
						dataType : "json"
					});
				}else{
				}
			});
		}else{
			$.messager.alert('更新', '请选择要删除的设备', 'warning');
		}
	}
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		var projectitemid = $("#projectitem").combobox('getValue');
		currentPro.projectitemid = projectitemid;
		var name = $("#name").textbox('getValue');
		currentPro.name = name;
		var startinterval = $("#startinterval").textbox('getValue');
		currentPro.startinterval = startinterval;
		var dailyinterval = $("#dailyinterval").textbox('getValue');
		currentPro.dailyinterval = dailyinterval;
		currentPro.code = $("#code").textbox('getValue');
		$.ajax({
			type : 'POST',
			url : "equipmentSave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存设备',
						msg : '保存设备成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
				}else{
					$.messager.alert('保存设备', '保存设备失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function addTab(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		var title = '压力记录-' + item.name;
		window.parent.window.addTab(title, "equipmentpressure.do?id=" + item.id, item.id);
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(),month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(),hour = date.getHours(),min = date.getMinutes(),sec = date.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
	$.extend($.fn.textbox.defaults.rules, {
		number : {//value值为文本框中的值
			validator : function(value) {
				var reg = /^\d+(\.\d+)?$/;
				return reg.test(value);
			},
			message : "请输入0-100之间的数字"
		},
		chinese : {
			validator : function(value,param) {
				var reg = /^[\u4e00-\u9fa5]+$/i;
				return reg.test(value);
			},
			message : "请输入中文"
		},
		checkLength : {
			validator : function(value,param) {
				return param[0] >= get_length(value);
			},
			message : '请输入最大{0}位字符'
		},
		specialCharacter : {
			validator : function(value,param) {
				var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
				return !reg.test(value);
			},
			message : '不允许输入特殊字符'
		},
		end : {
			validator : function(value,param) {
				var startDate = $("#startdate").val();
				var startTmp = new Date(startDate.replace(/-/g, "/"));
				var endTmp = new Date(value.replace(/-/g, "/"));
				return startTmp <= endTmp;
			},
			message : '结束时间不能早于开始时间！'
		},
		remainmoney : {
			validator : function(value,param) {
				return parseFloat(value) <= parseFloat(remainmoney);
			},
			message : '不能超过剩余金额'
		}
	});
	
</script>
</html>